package com.wanpinghui.supplier.message.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.message.nim.NimManager;
import com.wanpinghui.supplier.message.nim.NimUserExt;
import com.wanpinghui.supplier.message.nim.WPHMsg;
import com.wanpinghui.supplier.utils.WTimeUtil;
import com.wanpinghui.wphlibrary.model.message.bean.MessageType;
import com.wanpinghui.wphlibrary.model.message.bean.Messages;
import com.wanpinghui.wphlibrary.model.message.bean.RecentMsgBean;
import com.yunshl.yunshllibrary.recyclerview.BaseRecyclerViewAdapter;
import com.yunshl.yunshllibrary.utils.TextUtil;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * Created by xiongning on 17-12-1.
 */

public class RecentMsgAdapter extends BaseRecyclerViewAdapter {

    public RecentMsgAdapter(Context context) {
        super(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recent_msg, null);
        return new RecentHolder(view);
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder mViewHolder, int position) {
        super.onBindViewHolder(mViewHolder, position);
        RecentHolder holder = (RecentHolder) mViewHolder;
        RecentMsgBean rmb = (RecentMsgBean) datas.get(position);
        String account = rmb.getChatImId();
        NimUserInfo nui = NimManager.getInstance().getUser(account, null);
        if (nui != null) {
            account = nui.getName() == null ? account : nui.getName();
            if (nui.getExtensionMap() != null && nui.getExtensionMap().get("type") != null) {
                int type = (int) nui.getExtensionMap().get("type");
                if ( type == 1) {
                    account = "客户 | " + account;
                }
                else if(type==100){
                    account = "工程商 | " + account;
                }
            }
        }
        holder.tv_name.setText(account);
        Messages messages = Messages.getMessages(rmb.getMessage());
        if (messages == null)
            return;
        switch (rmb.getMsg_type()) {
            case MessageType.TYPE_NEW_OBJECT://新订单提醒
            case MessageType.TYPE_NEW_OBJECT_DISPATCH:
            case MessageType.TYPE_NEW_OBJECT_FREE_SEND:
                holder.tv_name.setText("新订单提醒");
                if (rmb.getMsg_type() == MessageType.TYPE_NEW_OBJECT_DISPATCH || rmb.getMsg_type() == MessageType.TYPE_NEW_OBJECT_FREE_SEND) {
                    String message ="";
                    if(rmb.getMsg_type() == MessageType.TYPE_NEW_OBJECT_DISPATCH)
                        message = "[派送订单]" + messages.getMsg().trim();
                    else
                        message = "[赠送订单]" + messages.getMsg().trim();
                    SpannableString spannableString = new SpannableString(message);
                    spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.colorPrimaryBlue)), 0, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                    holder.tv_content.setText(spannableString);
                } else {
                    holder.tv_content.setText(messages.getMsg().trim());
                }
                holder.iv_head.setImageResource(R.mipmap.message_icon_order);
                break;
            case MessageType.TYPE_NORMAL://普通聊天
                if (nui != null && TextUtil.isNotEmpty(nui.getAvatar()))
                    Glide.with(context).load(nui.getAvatar()).asBitmap().error(R.mipmap.message_icon_clientele)
                            .transform(new RoundedCornersTransformation(context, 3, 0)).into(holder.iv_head);
                else
                    holder.iv_head.setImageResource(R.mipmap.message_icon_clientele);
                switch (messages.getType()) {
                    case WPHMsg.MSG_DOC:
                        holder.tv_content.setText("文件消息");
                        break;
                    case WPHMsg.MSG_VOICE:
                        holder.tv_content.setText("语音消息");
                        break;
                    case WPHMsg.MSG_TXT:
                        holder.tv_content.setText(messages.getMsg());
                        break;
                    case WPHMsg.MSG_IMG:
                        holder.tv_content.setText("图片消息");
                        break;
                    case WPHMsg.MSG_ORDER:
                        holder.tv_content.setText("产品交易消息");
                        break;
                    case WPHMsg.MSG_BID_SUCCESS:
                        holder.tv_content.setText(messages.getMsg());
                        break;
                    case WPHMsg.MSG_BID_RECHARGE:
                        holder.tv_content.setText(messages.getMsg());
                        break;
                    case WPHMsg.MSG_BID_FOLLOW:
                        holder.tv_content.setText(messages.getMsg());
                        break;
                }
                break;
            case MessageType.TYPE_BID_SUCCESS://竞标成功提醒
                holder.tv_name.setText("竞拍成功");
                holder.tv_content.setText(messages.getMsg().trim());
                holder.iv_head.setImageResource(R.mipmap.message_icon_order);
                break;
            case MessageType.TYPE_SERVICE://客服
                holder.iv_head.setImageResource(R.mipmap.message_icon_service_head);
                holder.tv_name.setText("万屏汇客服");
                switch (messages.getType()) {
                    case WPHMsg.MSG_DOC:
                        holder.tv_content.setText("文件消息");
                        break;
                    case WPHMsg.MSG_VOICE:
                        holder.tv_content.setText("语音消息");
                        break;
                    case WPHMsg.MSG_TXT:
                        holder.tv_content.setText(messages.getMsg());
                        break;
                    case WPHMsg.MSG_IMG:
                        holder.tv_content.setText("图片消息");
                        break;
                    case WPHMsg.MSG_BID_SUCCESS:
                        holder.tv_content.setText(messages.getMsg());
                        break;
                    case WPHMsg.MSG_BID_RECHARGE:
                        holder.tv_content.setText(messages.getMsg());
                        break;
                    case WPHMsg.MSG_BID_FOLLOW:
                        holder.tv_content.setText(messages.getMsg());
                        break;
                }
                break;
        }
        int count = rmb.getUn_read_count();
        String countStr = count + "";
        if (count > 99)
            countStr = "99+";
        holder.tv_msg_num.setText(countStr);
        if (count <= 0)
            holder.tv_msg_num.setVisibility(View.GONE);
        else
            holder.tv_msg_num.setVisibility(View.VISIBLE);
        holder.tv_msg_time.setText(WTimeUtil.getRecentChatTime(rmb.getCreated_at()));
    }

    class RecentHolder extends RecyclerView.ViewHolder {
        ImageView iv_head;
        TextView tv_msg_num;
        TextView tv_name;
        TextView tv_content;
        TextView tv_msg_time;

        public RecentHolder(View itemView) {
            super(itemView);
            iv_head = (ImageView) itemView.findViewById(R.id.iv_head);
            tv_msg_num = (TextView) itemView.findViewById(R.id.tv_msg_num);
            tv_content = (TextView) itemView.findViewById(R.id.tv_content);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_msg_time = (TextView) itemView.findViewById(R.id.tv_msg_time);
            //itemView.setLayoutParams(new GridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
    }

}
